const express = require("express");
const router = express.Router();
const axios = require("axios");
let baseUrl = "http://localhost:5008/api/area";

// 获取所有区域
router.get("/getAreas", async (req, res, next) => {
  const { data } = await axios.get(`${baseUrl}/getAreas`, {
    headers: req.headers
  }
  );
  // console.log("result from server: ", data);
  res.send(data);
  next();
});

router.get("/getAreasByUserId", async (req, res, next) => {
  const { data } = await axios.get(`${baseUrl}/getAreasByUserId`, {
    params: {
      userId: req.query.userId
    },
    headers: req.headers
  }
  );
  // console.log("result from server: ", data);
  res.send(data);
  next();
});

// 刷新 token
router.post("/getUsersByAreaId", async (req, res, next) => {
  const { data } = await axios.post(`${baseUrl}/getUsersByAreaId`, {}, {
    params: {
      areaId: req.body.areaId
    },
    headers: req.headers
  }
  );
  // console.log("result from server: ", data);
  res.send(data);
  next();
});

router.post("/grant", async (req, res, next) => {
  console.log(req.body);
  const { data } = await axios.post(`${baseUrl}/grant`, {
    userId: req.body.userId,
    areaId: req.body.areaId,
  }, {
    headers: req.headers
  }
  );
  // console.log("result from server: ", data);
  res.send(data);
  next();
});

module.exports = router;